﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TddGenerator.Core.Domain;

namespace TddGenerator.Core.Services.Impl
{
    public class TokenFactory:ITokenFactory
    {
        private readonly IVsIdeService ideService;
        
        public TokenFactory(IVsIdeService vsIdeService)
        {
            this.ideService = vsIdeService;
        }

        public IDictionary<string, string> Create(ClassToCreate classToCreate, TemplateSet templateSet,
                                                  GeneratorConfiguration configuration,IList<string> ImplementationFolder)
        {
            Dictionary<string,string> tokens = new Dictionary<string, string>();
            
            tokens.Add("$concrete$",classToCreate.ClassName);
            
            tokens.Add("$concretevariable$",
                       CreateConcreteVariableName(classToCreate));
            tokens.Add("$interface$", configuration.InterfacePrefix + classToCreate.ClassName);
            
            string namespaceName = ideService.GetRootNamespace(classToCreate.ClassProject) +"."+
                                   string.Join(".", classToCreate.FolderHierarchy.ToArray());
            tokens.Add("$namespace$", namespaceName);
            
            string implementationNamespace = ideService.GetRootNamespace(classToCreate.ClassProject) +"."+
                                   string.Join(".", ImplementationFolder.ToArray());
            tokens.Add("$implementationnamespace$",implementationNamespace);

            string interfaceNamespace = ideService.GetRootNamespace(classToCreate.TestProject)+"." +
                                   string.Join(".", classToCreate.FolderHierarchy.ToArray());
            tokens.Add("$testnamespace$", interfaceNamespace);
            
            tokens.Add("$testsuffix$", configuration.TestSufix);

            return tokens;
        }

        private string CreateConcreteVariableName(ClassToCreate classToCreate)
        {
            return classToCreate.ClassName.Substring(0, 1).ToLower() + classToCreate.ClassName.Substring(1, classToCreate.ClassName.Length - 1);
        }
    }
}
